<?php

namespace web\user\controller;

/**
 * User公共控制器
 */
class Base extends \web\common\controller\BaseController {

    /**
     * 是否管理员(1=是)
     * @var type 
     */
    protected $is_admin = 0;
    protected $store_id = 0;
    

    /**
     * 账号所属类型(0=总部)
     * @var type 
     */
    protected $user_type = 0;

    /**
     * 登录账号ID
     * @var type 
     */
    protected $login_user_id = 0;

    /**
     * 登录账号
     * @var type 
     */
    protected $login_user_name = '';


    /**
     * 可操作的权限IDS。
     * @var type 
     */
    protected $addon = '';
    protected $module = '';
    protected $controller = '';
    protected $base_view_path = '';

    const LOG_ADD = 0;
    const LOG_DEL = 1;
    const LOG_EDIT = 2;
    const LOG_APPR = 3; //审核
    const LOG_CANCEL_APPR = 4; //反审核

    /**
     * CDN图片域名;
     * @var type 
     */
    protected $img_base_url = '';

    protected function _initialize() {

        $server_name = $_SERVER['SERVER_NAME'];
//        echo $server_name;
//         if ($server_name != 'xxkj.ifc008.com') {
//             session('loginData', '');
//             exit('error');
//         }

//         $wrongIp = '59.33.195.176';
//         $loginIp = getRealIpAddr();
// //        if ($wrongIp == $loginIp || $loginIp != '120.39.40.104'){
//         if ($wrongIp == $loginIp){
//             $url = url('/user/login');
//             session('loginData', '');
//             $this->redirect($url);
//             exit;
//         }
        $loginData = session('loginData');
        $uid = '';
        $username = '';
        if (!empty($loginData)) {
            $uid = $loginData['uid'];
            $username = $loginData['username'];
        }
        if (empty($uid) || empty($username)) {
            //登录超时跳转登录
            $url = url('/user/login');
            $this->redirect($url);
            exit;
        }
        parent::_initialize();
        $this->is_admin = $loginData['is_admin'];
        $this->login_user_id = $uid;
        if(isset($loginData['store_id'])){
            $this->store_id = $loginData['store_id'];
        }
        $this->login_user_name = $username;
        $this->user_type = $loginData['user_type'];
        $this->addons_ids = $loginData['addons_ids'];
    
        if (!IS_AJAX || IS_PJAX) {
            $this->base_view_path = $this->view_path;
            $addon = '';
            if (defined('ADDON_NAME')) {
                $addon = ADDON_NAME;
            }
            $__c = explode('.', CONTROLLER_NAME);
            if (count($__c) > 1)
                $controller = $__c[1];
            else
                $controller = $__c[0];
            $__controller = \think\Loader::parseName($controller); //controller
            if ($this->is_admin != 1 && $this->user_type == 0) {
                //后台角色账号
                if ($addon != null && $addon != '' && $addon != 'editor' && $addon != 'uploader' && $addon != 'resources') {
                    $plugin = $addon . '/' . $__controller;
                    if (strstr($loginData['promission'], $plugin) === false) {
                        $this->tips('您无此插件使用权限');
                    }
                }
            }
            $menu = new \web\common\model\sys\UserNavMenuModel();
            $this->addon = $addon;
            $this->module = MODULE_NAME;
            $this->controller = $__controller;
            $this->assign('_CONTROLLER_NAME', $__controller);
            $this->assign('_ADDON_NAME', $addon);
            $templateConfig = config('template');
            $suffix = ltrim($templateConfig['view_suffix'], '.');
            //注册模板模块
            $this->assign('PUBLIC_HEADER', APP_PATH . MODULE_NAME . DS . 'view' . DS . 'default' . DS . 'public' . DS . 'header' . '.' . $suffix);

            $this->setLoadDataAction('');
            $menuData = $menu->getMenuData($addon, $__controller);
            $addon_id = 0;
            $menu_nav = '';
            if (!empty($menuData)) {
                $addon_id = $menuData['id'];
                $menu_nav = $menuData['title'];
            }
            $this->assign('addon_id', $addon_id);
            $page = 0;
            if ($this->_get('page') != '')
                $page = $this->_get('page');
            $this->assign('page', $page);
            $filter = '';
            if ($this->_get('filter') != '')
                $filter = $this->_get('filter');
            $this->assign('page_nav', $menu_nav);
            $this->assign('addons_ids', $loginData['addons_ids']);
            $this->assign('filter', $filter);
            $this->assign('is_admin', $this->is_admin);
            $this->assign('user_type', $this->user_type);
            $this->assign('login_user_name', $this->login_user_name);
        }
    }

    /**
     * 提示信息。
     * @param string $msg
     * @param type $url
     * @param type $data
     * @param type $wait
     * @param array $header
     * @throws HttpResponseException
     */
    protected function tips($msg = '', $url = null, $data = '', $wait = 3, array $header = []) {
        $code = 0;
        if (is_numeric($msg)) {
            $code = $msg;
            $msg = '';
        }
        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
            'url' => $url
        ];
        $type = $this->getResponseType();
        $path = $this->base_view_path . 'tips.html';
        $this->assign('result', $result);
        $content = $this->fetch($path, $result);
        $response = \think\Response::create($content, $type)->header($header);
        throw new \think\exception\HttpResponseException($response);
    }

    protected function fetch($template = '', $vars = [], $replace = [], $config = []) {
        $conent = parent::fetch($template, $vars, $replace, $config);
        if (IS_PJAX) {
            $html = '<addon>' . $this->addon . '</addon>';
            $html .= '<module>' . $this->module . '</module>';
            $html .= '<controller>' . $this->controller . '</controller>';
            $conent = $html . $conent;
        }
        return $conent;
    }

    /**
     * 加载表单的方法名称
     * @param type $value
     */
    protected function setLoadDataAction($value) {
        $this->assign('loadDataAction', $value);
    }

    /**
     * 获取当前页码
     * @return type
     */
    protected function getPageIndex() {
        $pageIndex = $this->_get('page');
        if (empty($pageIndex))
            $pageIndex = -1;
        return $pageIndex;
    }

    /**
     * 获取每页显示数量
     * @return type
     */
    protected function getPageSize() {
        $pageSize = $this->_get('rows');
        if (empty($pageSize))
            $pageSize = -1;
        if ($pageSize > 50)
            $pageSize = 50;
        return $pageSize;
    }

    /**
     * 获取排序信息
     * @param type $orderBy 默认排序信息
     * @return string
     */
    protected function getOrderBy($orderBy) {
        $sort = $this->_get('sort');
        $order = $this->_get('order');
        if (!empty($sort) && !empty($order)) {
            $sortArr = explode(',', $sort);
            $orderArr = explode(',', $order);
            $i = 0;
            $s = '';
            foreach ($sortArr as $field) {
                if ($i > 0)
                    $s .= ',';
                $s .= $field . ' ' . $orderArr[$i];
                $i++;
            }
            $orderBy = $s;
        }
        return $orderBy;
    }

    /**
     * 返回DataGrid数据
     * @param type $total
     * @param type $rows     
     */
    protected function toDataGrid($total, $rows) {
        if (empty($rows))
            $rows = array();
        $data = array('total' => $total, 'rows' => $rows);
        return $data;
    }

    /**
     * 返回DataGrid数据
     * @param type $total
     * @param type $rows     
     */
    protected function toTotalDataGrid($total, $rows, $count_total) {
        if (empty($rows))
            $rows = array();
        $data = array('total' => $total, 'rows' => $rows, 'count_total' => $count_total);
        return $data;
    }

    /**
     * 返回支付统计
     * @param $total
     * @param $rows
     * @param $rmb_total
     * @param $ime_total
     * @return array
     */
    protected function toTotalSacn($total, $rows, $rmb_total, $ime_total) {
        if (empty($rows))
            $rows = array();
        $data = array('total' => $total, 'rows' => $rows, 'rmb_total' => $rmb_total, 'ime_total' => $ime_total);
        return $data;
    }

    /**
     * 会员首页统计
     * @param $total
     * @param $rows
     * @param $total_usdt
     * @param $total_bean
     * @param $total_freeze
     * @return array
     */
    protected function memberGrid($total, $rows, $total_usdt, $total_bean, $total_freeze) {
        if (empty($rows))
            $rows = array();
        $data = array('total' => $total, 'rows' => $rows, 'total_usdt' => $total_usdt, 'total_bean' => $total_bean, 'total_freeze' => $total_freeze);
        return $data;
    }

    /**
     * 添加管理日志
     * @param type $type    0=增，1=删，2=改
     * @param type $action_name 方法名称
     * @param type $data    数据
     * @param type $remark  备注
     */
    protected function addLog($type, $action_name, $_data, $user_id = '', $remark = '') {
        $data['type'] = $type;
        $data['action_name'] = $action_name;
        $data['data'] = json_encode($_data, JSON_UNESCAPED_UNICODE);
        $data['remark'] = $remark;
        if ($user_id != '') {
            $u = new \addons\member\model\MemberAccountModel();
            $member_name = $u->where('id=' . $user_id)->value('username');
            $data['user_id'] = $user_id;
            $data['member_name'] = $member_name;
        }

        $data['admin_id'] = $this->login_user_id;
        $data['username'] = $this->login_user_name;
        $data['addon'] = ADDON_NAME;
        $data['controller'] = CONTROLLER_NAME;
        $data['action'] = ACTION_NAME;
        $data['update_time'] = NOW_DATETIME;
        $m = new \addons\config\model\Log();
        return $m->add($data, false);
    }

}
